home *** CD-ROM | disk | FTP | other *** search
/ Suzy B Software 2 / Suzy B Software CD-ROM 2 (1994).iso / new_file / mintprgs / mint112s / mint112s.lzh / pipefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-14  |  29.2 KB  |  1,224 lines

  1. /*
  2. Copyright 1991,1992 Eric R. Smith.
  3. Copyright 1992,1993,1994 Atari Corporation.
  4. All rights reserved.
  5.  */
  6.  
  7. /* simple pipefs.c */
  8.  
  9. #include "mint.h"
  10.  
  11. static int pipetime, pipedate;    /* root directory time/date stamp */
  12.  
  13. static long    ARGS_ON_STACK pipe_root    P_((int drv, fcookie *fc));
  14. static long    ARGS_ON_STACK pipe_lookup    P_((fcookie *dir, const char *name, fcookie *fc));
  15. static long    ARGS_ON_STACK pipe_getxattr    P_((fcookie *file, XATTR *xattr));
  16. static long    ARGS_ON_STACK pipe_chattr    P_((fcookie *file, int attrib));
  17. static long    ARGS_ON_STACK pipe_chown    P_((fcookie *file, int uid, int gid));
  18. static long    ARGS_ON_STACK pipe_chmode    P_((fcookie *file, unsigned mode));
  19. static long    ARGS_ON_STACK pipe_rmdir    P_((fcookie *dir, const char *name));
  20. static long    ARGS_ON_STACK pipe_remove    P_((fcookie *dir, const char *name));
  21. static long    ARGS_ON_STACK pipe_getname    P_((fcookie *root, fcookie *dir,
  22.                             char *pathname, int size));
  23. static long    ARGS_ON_STACK pipe_rename    P_((fcookie *olddir, char *oldname,
  24.                     fcookie *newdir, const char *newname));
  25. static long    ARGS_ON_STACK pipe_opendir    P_((DIR *dirh, int flags));
  26. static long    ARGS_ON_STACK pipe_readdir    P_((DIR *dirh, char *nm, int nmlen, fcookie *));
  27. static long    ARGS_ON_STACK pipe_rewinddir    P_((DIR *dirh));
  28. static long    ARGS_ON_STACK pipe_closedir    P_((DIR *dirh));
  29. static long    ARGS_ON_STACK pipe_pathconf    P_((fcookie *dir, int which));
  30. static long    ARGS_ON_STACK pipe_dfree    P_((fcookie *dir, long *buf));
  31. static long    ARGS_ON_STACK pipe_creat    P_((fcookie *dir, const char *name, unsigned mode,
  32.                     int attrib, fcookie *fc));
  33. static DEVDRV *    ARGS_ON_STACK pipe_getdev    P_((fcookie *fc, long *devsp));
  34.  
  35. static long    ARGS_ON_STACK pipe_open    P_((FILEPTR *f));
  36. static long    ARGS_ON_STACK pipe_write    P_((FILEPTR *f, const char *buf, long bytes));
  37. static long    ARGS_ON_STACK pipe_read    P_((FILEPTR *f, char *buf, long bytes));
  38. static long    ARGS_ON_STACK pty_write        P_((FILEPTR *f, const char *buf, long bytes));
  39. static long    ARGS_ON_STACK pty_read    P_((FILEPTR *f, char *buf, long bytes));
  40. static long    ARGS_ON_STACK pty_writeb    P_((FILEPTR *f, const char *buf, long bytes));
  41. static long    ARGS_ON_STACK pty_readb    P_((FILEPTR *f, char *buf, long bytes));
  42. static long    ARGS_ON_STACK pipe_lseek    P_((FILEPTR *f, long where, int whence));
  43. static long    ARGS_ON_STACK pipe_ioctl    P_((FILEPTR *f, int mode, void *buf));
  44. static long    ARGS_ON_STACK pipe_datime    P_((FILEPTR *f, short *time, int rwflag));
  45. static long    ARGS_ON_STACK pipe_close    P_((FILEPTR *f, int pid));
  46. static long    ARGS_ON_STACK pipe_select    P_((FILEPTR *f, long p, int mode));
  47. static void    ARGS_ON_STACK pipe_unselect    P_((FILEPTR *f, long p, int mode));
  48.  
  49. DEVDRV pty_device = {
  50.     pipe_open, pty_write, pty_read, pipe_lseek, pipe_ioctl, pipe_datime,
  51.     pipe_close, pipe_select, pipe_unselect, pty_writeb, pty_readb
  52. };
  53.  
  54. DEVDRV pipe_device = {
  55.     pipe_open, pipe_write, pipe_read, pipe_lseek, pipe_ioctl, pipe_datime,
  56.     pipe_close, pipe_select, pipe_unselect
  57. };
  58.  
  59.  
  60. FILESYS pipe_filesys = {
  61.     (FILESYS *)0,
  62.     0,
  63.     pipe_root,
  64.     pipe_lookup, pipe_creat, pipe_getdev, pipe_getxattr,
  65.     pipe_chattr, pipe_chown, pipe_chmode,
  66.     nomkdir, pipe_rmdir, pipe_remove, pipe_getname, pipe_rename,
  67.     pipe_opendir, pipe_readdir, pipe_rewinddir, pipe_closedir,
  68.     pipe_pathconf, pipe_dfree,
  69.     nowritelabel, noreadlabel, nosymlink, noreadlink,
  70.     nohardlink, nofscntl, nodskchng
  71. };
  72.  
  73. /* size of pipes */
  74. #define PIPESIZ    4096        /* MUST be a multiple of 4 */
  75.  
  76. /* writes smaller than this are atomic */
  77. #define PIPE_BUF 1024        /* should be a multiple of 4 */
  78.  
  79. /* magic flag: indicates that nobody but the creator has opened this pipe */
  80. /* note: if this many processes open the pipe, we lose :-( */
  81. #define VIRGIN_PIPE    0x7fff
  82.  
  83. struct pipe {
  84.     int    readers;    /* number of readers of this pipe */
  85.     int    writers;    /* number of writers of this pipe */
  86.     int    start, len;    /* pipe head index, size */
  87.     long    rsel;        /* process that did select() for reads */
  88.     long    wsel;        /* process that did select() for writes */
  89.     char    buf[PIPESIZ];    /* pipe data */
  90. };
  91.  
  92. struct fifo {
  93.     char    name[NAME_MAX+1]; /* FIFO's name */
  94.     short    date, time;    /* date & time of last write */
  95.     short    dosflags;    /* DOS flags, e.g. FA_RDONLY, FA_HIDDEN */
  96.     ushort    mode;        /* file access mode, for XATTR */
  97.     ushort    uid, gid;    /* file owner; uid and gid */
  98.     short    flags;        /* various other flags (e.g. O_TTY) */
  99.     short    lockpid;    /* pid of locking process */
  100.     short    cursrate;    /* cursor flash rate for pseudo TTY's */
  101.     struct tty *tty;    /* tty struct for pseudo TTY's */
  102.     struct pipe *inp;    /* pipe for reads */
  103.     struct pipe *outp;    /* pipe for writes (0 if unidirectional) */
  104.     struct fifo *next;    /* link to next FIFO in list */
  105.     FILEPTR *open;        /* open file pointers for this fifo */
  106. } *rootlist;
  107.  
  108.  
  109. static long ARGS_ON_STACK 
  110. pipe_root(drv, fc)
  111.     int drv;
  112.     fcookie *fc;
  113. {
  114.     if (drv == PIPEDRV) {
  115.         fc->fs = &pipe_filesys;
  116.         fc->dev = drv;
  117.         fc->index = 0L;
  118.         return 0;
  119.     }
  120.     fc->fs = 0;
  121.     return EINTRN;
  122. }
  123.  
  124. static long ARGS_ON_STACK 
  125. pipe_lookup(dir, name, fc)
  126.     fcookie *dir;
  127.     const char *name;
  128.     fcookie *fc;
  129. {
  130.     struct fifo *b;
  131.  
  132.     TRACE(("pipe_lookup(%s)", name));
  133.  
  134.     if (dir->index != 0) {
  135.         DEBUG(("pipe_lookup(%s): bad directory", name));
  136.         return EPTHNF;
  137.     }
  138. /* special case: an empty name in a directory means that directory */
  139. /* so does "." */
  140.     if (!*name || (name[0] == '.' && name[1] == 0)) {
  141.         *fc = *dir;
  142.         return 0;
  143.     }
  144.  
  145. /* another special case: ".." could be a mount point */
  146.     if (!strcmp(name, "..")) {
  147.         *fc = *dir;
  148.         return EMOUNT;
  149.     }
  150.  
  151.     for (b = rootlist; b; b = b->next) {
  152.         if (!strnicmp(b->name, name, NAME_MAX)) {
  153.             fc->fs = &pipe_filesys;
  154.             fc->index = (long)b;
  155.             fc->dev = dir->dev;
  156.             return 0;
  157.         }
  158.     }
  159.     DEBUG(("pipe_lookup: name `%s' not found", name));
  160.     return EFILNF;
  161. }
  162.  
  163. static long ARGS_ON_STACK 
  164. pipe_getxattr(fc, xattr)
  165.     fcookie *fc;
  166.     XATTR *xattr;
  167. {
  168.     struct fifo *this;
  169.  
  170.     xattr->index = fc->index;
  171.     xattr->dev = fc->dev;
  172.     xattr->rdev = fc->dev;
  173.     xattr->nlink = 1;
  174.     xattr->blksize = 1024L;
  175.  
  176.     if (fc->index == 0) {        /* root directory? */
  177.         xattr->uid = xattr->gid = 0;
  178.         xattr->mtime = xattr->atime = xattr->ctime = pipetime;
  179.         xattr->mdate = xattr->adate = xattr->cdate = pipedate;
  180.         xattr->mode = S_IFDIR | DEFAULT_DIRMODE;
  181.         xattr->attr = FA_DIR;
  182.         xattr->size = xattr->nblocks = 0;
  183.     } else {
  184.         this = (struct fifo *)fc->index;
  185.         xattr->uid = this->uid;
  186.         xattr->gid = this->gid;
  187.         xattr->mtime = xattr->atime = xattr->ctime = this->time;
  188.         xattr->mdate = xattr->adate = xattr->cdate = this->date;
  189.         xattr->mode = this->mode;
  190.         xattr->attr = this->dosflags;
  191.     /* note: fifo's that haven't been opened yet can be written to */
  192.         if (this->flags & O_HEAD) {
  193.             xattr->attr &= ~FA_RDONLY;
  194.         }
  195.  
  196.         if (this->dosflags & FA_SYSTEM) {    /* pseudo-tty */
  197.             xattr->size = PIPESIZ/4;
  198.             xattr->rdev = PIPE_RDEV|1;
  199.         } else {
  200.             xattr->size = PIPESIZ;
  201.             xattr->rdev = PIPE_RDEV|0;
  202.         }
  203.         xattr->nblocks = xattr->size / 1024L;
  204.     }
  205.     return 0;
  206. }
  207.  
  208. static long ARGS_ON_STACK 
  209. pipe_chattr(fc, attrib)
  210.     fcookie *fc;
  211.     int attrib;
  212. {
  213.     UNUSED(fc); UNUSED(attrib);
  214.     return EACCDN;
  215. }
  216.  
  217. static long ARGS_ON_STACK 
  218. pipe_chown(fc, uid, gid)
  219.     fcookie *fc;
  220.     int uid, gid;
  221. {
  222.     struct fifo *this;
  223.  
  224.     if ((this = (struct fifo *)fc->index) == 0)
  225.         return EACCDN;
  226.  
  227.     this->uid = uid;
  228.     this->gid = gid;
  229.     return 0;
  230. }
  231.  
  232. static long ARGS_ON_STACK 
  233. pipe_chmode(fc, mode)
  234.     fcookie *fc;
  235.     unsigned mode;
  236. {
  237.     struct fifo *this;
  238.  
  239.     if ((this = (struct fifo *)fc->index) == 0)
  240.         return EACCDN;
  241.  
  242.     this->mode = (this->mode & S_IFMT) | (mode & ~S_IFMT);
  243.     return 0;
  244. }
  245.  
  246. static long ARGS_ON_STACK 
  247. pipe_rmdir(dir, name)
  248.     fcookie *dir;
  249.     const char *name;
  250. {
  251.     UNUSED(dir); UNUSED(name);
  252.  
  253. /* the kernel already checked to see if the file exists */
  254.     return EACCDN;
  255. }
  256.  
  257. static long ARGS_ON_STACK 
  258. pipe_remove(dir, name)
  259.     fcookie *dir;
  260.     const char *name;
  261. {
  262.     UNUSED(dir); UNUSED(name);
  263.  
  264. /* the kernel already checked to see if the file exists */
  265.     return EACCDN;
  266. }
  267.  
  268. static long ARGS_ON_STACK 
  269. pipe_getname(root, dir, pathname, size)
  270.     fcookie *root, *dir; char *pathname;
  271.     int size;
  272. {
  273.     UNUSED(root);
  274.     UNUSED(size);    /* BUG: we should support 'size' */
  275.  
  276.     if (dir->index == 0)
  277.         *pathname = 0;
  278.     else
  279.         strcpy(pathname, ((struct fifo *)dir->index)->name);
  280.     return 0;
  281. }
  282.  
  283. static long ARGS_ON_STACK 
  284. pipe_rename(olddir, oldname, newdir, newname)
  285.     fcookie *olddir;
  286.     char *oldname;
  287.     fcookie *newdir;
  288.     const char *newname;
  289. {
  290.     UNUSED(olddir); UNUSED(oldname);
  291.     UNUSED(newdir); UNUSED(newname);
  292.  
  293.     return EACCDN;
  294. }
  295.  
  296. static long ARGS_ON_STACK 
  297. pipe_opendir(dirh, flags)
  298.     DIR *dirh;
  299.     int flags;
  300. {
  301.     UNUSED(flags);
  302.  
  303.     if (dirh->fc.index != 0) {
  304.         DEBUG(("pipe_opendir: bad directory"));
  305.         return EPTHNF;
  306.     }
  307.     dirh->index = 0;
  308.     return 0;
  309. }
  310.  
  311. static long ARGS_ON_STACK 
  312. pipe_readdir(dirh, name, namelen, fc)
  313.     DIR *dirh;
  314.     char *name;
  315.     int namelen;
  316.     fcookie *fc;
  317. {
  318.     struct fifo *this;
  319.     int i;
  320.     int giveindex = dirh->flags == 0;
  321.  
  322.     i = dirh->index++;
  323.     this = rootlist;
  324.     while (i > 0 && this) {
  325.         --i; this = this->next;
  326.     }
  327.     if (!this)
  328.         return ENMFIL;
  329.  
  330.     fc->fs = &pipe_filesys;
  331.     fc->index = (long)this;
  332.     fc->dev = dirh->fc.dev;
  333.     if (giveindex) {
  334.         namelen -= (int) sizeof(long);
  335.         if (namelen <= 0) return ERANGE;
  336.         *((long *)name) = (long)this;
  337.         name += sizeof(long);
  338.     }
  339.     strncpy(name, this->name, namelen-1);
  340.     if (strlen(this->name) >= namelen)
  341.         return ENAMETOOLONG;
  342.     return 0;
  343. }
  344.  
  345. static long ARGS_ON_STACK 
  346. pipe_rewinddir(dirh)
  347.     DIR *dirh;
  348. {
  349.     dirh->index = 0;
  350.     return 0;
  351. }
  352.  
  353. static long ARGS_ON_STACK 
  354. pipe_closedir(dirh)
  355.     DIR *dirh;
  356. {
  357.     UNUSED(dirh);
  358.     return 0;
  359. }
  360.  
  361. static long ARGS_ON_STACK 
  362. pipe_pathconf(dir, which)
  363.     fcookie *dir;
  364.     int which;
  365. {
  366.     UNUSED(dir);
  367.  
  368.     switch(which) {
  369.     case -1:
  370.         return DP_MAXREQ;
  371.     case DP_IOPEN:
  372.         return UNLIMITED;    /* no internal limit on open files */
  373.     case DP_MAXLINKS:
  374.         return 1;        /* no hard links */
  375.     case DP_PATHMAX:
  376.         return PATH_MAX;
  377.     case DP_NAMEMAX:
  378.         return NAME_MAX;
  379.     case DP_ATOMIC:
  380.     /* BUG: for pty's, this should actually be PIPE_BUF/4 */
  381.         return PIPE_BUF;
  382.     case DP_TRUNC:
  383.         return DP_AUTOTRUNC;
  384.     case DP_CASE:
  385.         return DP_CASEINSENS;
  386.     case DP_MODEATTR:
  387.         return (0777L << 8)|
  388.                 DP_FT_DIR|DP_FT_FIFO;
  389.     case DP_XATTRFIELDS:
  390.         return DP_INDEX|DP_DEV|DP_NLINK|DP_UID|DP_GID|DP_MTIME;
  391.     default:
  392.         return EINVFN;
  393.     }
  394. }
  395.  
  396. static long ARGS_ON_STACK 
  397. pipe_dfree(dir, buf)
  398.     fcookie *dir;
  399.     long *buf;
  400. {
  401.     int i;
  402.     struct fifo *b;
  403.     long freemem;
  404.  
  405.     UNUSED(dir);
  406.  
  407. /* the "sector" size is the number of bytes per pipe */
  408. /* so we get the total number of sectors used by counting pipes */
  409.  
  410.     i = 0;
  411.     for (b = rootlist; b; b = b->next) {
  412.         if (b->inp) i++;
  413.         if (b->outp) i++;
  414.     }
  415.  
  416.     freemem = tot_rsize(core, 0) + tot_rsize(alt, 0);
  417.  
  418. /* note: the "free clusters" isn't quite accurate, since there's overhead
  419.  * in the fifo structure; but we're not looking for 100% accuracy here
  420.  */
  421.     buf[0] = freemem/PIPESIZ;    /* number of free clusters */
  422.     buf[1] = buf[0]+i;        /* total number of clusters */
  423.     buf[2] = PIPESIZ;        /* sector size (bytes) */
  424.     buf[3] = 1;            /* cluster size (sectors) */
  425.     return 0;
  426. }
  427.  
  428. /* create a new pipe.
  429.  * this only gets called by the kernel if a lookup already failed,
  430.  * so we know that the new pipe creation is OK
  431.  */
  432.  
  433. static long ARGS_ON_STACK 
  434. pipe_creat(dir, name, mode, attrib, fc)
  435.     fcookie *dir;
  436.     const char *name;
  437.     unsigned mode;
  438.     int attrib;
  439.     fcookie *fc;
  440. {
  441.     struct pipe *inp, *outp;
  442.     struct tty *tty;
  443.     struct fifo *b;
  444. /* selfread == 1 if we want reads to wait even if no other processes
  445.    have currently opened the file, and writes to succeed in the same
  446.    event. This is useful for servers who want to wait for requests.
  447.    Pipes should always have selfread == 0.
  448. */
  449.     int selfread = (attrib & FA_HIDDEN) ? 0 : 1;
  450.  
  451.  
  452.     /* create the new pipe */
  453.     if (0 == (inp = (struct pipe *)kmalloc(SIZEOF(struct pipe)))) {
  454.         return ENSMEM;
  455.     }
  456.     if (attrib & FA_RDONLY) {    /* read only FIFOs are unidirectional */
  457.         outp = 0;
  458.     } else {
  459.         outp = (struct pipe *)kmalloc(SIZEOF(struct pipe));
  460.         if (!outp) {
  461.             kfree(inp);
  462.             return ENSMEM;
  463.         }
  464.     }
  465.     b = (struct fifo *)kmalloc(SIZEOF(struct fifo));
  466.     if (!b) {
  467.         kfree(inp);
  468.         if (outp) kfree(outp);
  469.         return ENSMEM;
  470.     }
  471.     if (attrib & FA_SYSTEM) {    /* pseudo-tty */
  472.         tty = (struct tty *)kmalloc(SIZEOF(struct tty));
  473.         if (!tty) {
  474.             kfree(inp);
  475.             kfree(b);
  476.             if (outp) kfree(outp);
  477.             return ENSMEM;
  478.         }
  479.         tty->use_cnt = 0;
  480.         tty->rsel = tty->wsel = 0;
  481.             /* do_open does the rest of tty initialization */
  482.     } else tty = 0;
  483.  
  484. /* set up the pipes appropriately */
  485.     inp->start = inp->len = 0;
  486.     inp->readers = selfread ? 1 : VIRGIN_PIPE; inp->writers = 1;
  487.     inp->rsel = inp->wsel = 0;
  488.     if (outp) {
  489.         outp->start = outp->len = 0;
  490.         outp->readers = 1; outp->writers = selfread ? 1 : VIRGIN_PIPE;
  491.         outp->wsel = outp->rsel = 0;
  492.     }
  493.     strncpy(b->name, name, NAME_MAX);
  494.     b->name[NAME_MAX] = '\0';
  495.     b->time = timestamp;
  496.     b->date = datestamp;
  497.     b->dosflags = attrib;
  498.     b->mode = ((attrib & FA_SYSTEM) ? S_IFCHR : S_IFIFO) | (mode & ~S_IFMT);
  499.     b->uid = curproc->euid;
  500.     b->gid = curproc->egid;
  501.  
  502. /* the O_HEAD flag indicates that the file hasn't actually been opened
  503.  * yet; the next open gets to be the pty master. pipe_open will
  504.  * clear the flag when this happens.
  505.  */
  506.     b->flags = ((attrib & FA_SYSTEM) ? O_TTY : 0) | O_HEAD;
  507.     b->inp = inp; b->outp = outp; b->tty = tty;
  508.  
  509.     b->next = rootlist;
  510.     b->open = (FILEPTR *)0;
  511.     rootlist = b;
  512.  
  513. /* we have to return a file cookie as well */
  514.     fc->fs = &pipe_filesys;
  515.     fc->index = (long)b;
  516.     fc->dev = dir->dev;
  517.  
  518. /* update time/date stamps for u:\pipe */
  519.     pipetime = timestamp;
  520.     pipedate = datestamp;
  521.  
  522.     return 0;
  523. }
  524.  
  525. static DEVDRV * ARGS_ON_STACK 
  526. pipe_getdev(fc, devsp)
  527.     fcookie *fc;
  528.     long *devsp;
  529. {
  530.     struct fifo *b = (struct fifo *)fc->index;
  531.  
  532.     UNUSED(devsp);
  533.     return (b->flags & O_TTY) ? &pty_device : &pipe_device;
  534. }
  535.  
  536. /*
  537.  * PIPE device driver
  538.  */
  539.  
  540. static long ARGS_ON_STACK 
  541. pipe_open(f)
  542.     FILEPTR *f;
  543. {
  544.     struct fifo *p;
  545.     int rwmode = f->flags & O_RWMODE;
  546.  
  547.     p = (struct fifo *)f->fc.index;
  548.     f->flags |= p->flags;
  549. /*
  550.  * if this is the first open for this file, then the O_HEAD flag is
  551.  * set in p->flags. If not, and someone was trying to create the file,
  552.  * return an error
  553.  */
  554.     if (p->flags & O_HEAD) {
  555.         if (!(f->flags & O_CREAT)) {
  556.             DEBUG(("pipe_open: file hasn't been created yet"));
  557.             return EINTRN;
  558.         }
  559.         p->flags &= ~O_HEAD;
  560.     } else {
  561.         if (f->flags & O_CREAT) {
  562.             DEBUG(("pipe_open: fifo already exists"));
  563.             return EACCDN;
  564.         }
  565.     }
  566. /*
  567.  * check for file sharing compatibility. note that O_COMPAT gets mutated
  568.  * into O_DENYNONE, because any old programs that know about pipes will
  569.  * already handle multitasking correctly
  570.  */
  571.     if ( (f->flags & O_SHMODE) == O_COMPAT ) {
  572.         f->flags = (f->flags & ~O_SHMODE) | O_DENYNONE;
  573.     }
  574.     if (denyshare(p->open, f))
  575.         return EACCDN;
  576.     f->next = p->open;        /* add this open fileptr to the list */
  577.     p->open = f;
  578.  
  579. /*
  580.  * add readers/writers to the list
  581.  */
  582.     if (!(f->flags & O_HEAD)) {
  583.         if (rwmode == O_RDONLY || rwmode == O_RDWR) {
  584.             if (p->inp->readers == VIRGIN_PIPE)
  585.                 p->inp->readers = 1;
  586.             else
  587.                 p->inp->readers++;
  588.         }
  589.         if ((rwmode == O_WRONLY || rwmode == O_RDWR) && p->outp) {
  590.             if (p->outp->writers == VIRGIN_PIPE)
  591.                 p->outp->writers = 1;
  592.             else
  593.                 p->outp->writers++;
  594.         }
  595.     }
  596.  
  597. /* TTY devices need a tty structure in f->devinfo */
  598.     f->devinfo = (long)p->tty;
  599.  
  600.     return 0;
  601. }
  602.  
  603. static long ARGS_ON_STACK 
  604. pipe_write(f, buf, nbytes)
  605.     FILEPTR *f; const char *buf; long nbytes;
  606. {
  607.     int plen, j;
  608.     char *pbuf;
  609.     struct pipe *p;
  610.     struct fifo *this;
  611.     long bytes_written = 0;
  612.     long r;
  613.  
  614.     this = (struct fifo *)f->fc.index;
  615.     p = (f->flags & O_HEAD) ? this->inp : this->outp;
  616.     if (!p) {
  617.         DEBUG(("pipe_write: write on wrong end of pipe"));
  618.         return EACCDN;
  619.     }
  620.  
  621.     if (nbytes > 0 && nbytes <= PIPE_BUF) {
  622. check_atomicity:
  623.         if (is_terminal(f) && !(f->flags & O_HEAD) &&
  624.             (this->tty->state & TS_HOLD)) {
  625.             if (f->flags & O_NDELAY)
  626.                 return 0;
  627.             sleep (IO_Q, (long)&this->tty->state);
  628.             goto check_atomicity;
  629.         }
  630.         r = PIPESIZ - p->len; /* r is the number of bytes we can write */
  631.         if (r < nbytes) {
  632.     /* check for broken pipes */
  633.             if (p->readers == 0 || p->readers == VIRGIN_PIPE) {
  634.                 check_sigs();
  635.                 DEBUG(("pipe_write: broken pipe"));
  636.                 raise(SIGPIPE);
  637.                 return EPIPE;
  638.             }
  639. /* wake up any readers, and wait for them to gobble some data */
  640.             if (p->rsel) {
  641.                 wakeselect(p->rsel);
  642. #if 0
  643.                 p->rsel = 0;
  644. #endif
  645.             }
  646.             wake(IO_Q, (long)p);
  647.             sleep(IO_Q, (long)p);
  648.             goto check_atomicity;
  649.         }
  650.     }
  651.  
  652.     while (nbytes > 0) {
  653.         plen = p->len;
  654.         if (plen < PIPESIZ) {
  655.             pbuf = &p->buf[(p->start + plen) & (PIPESIZ - 1)];
  656.             /* j is the amount that can be written continuously */
  657.             j = (int)(PIPESIZ - (pbuf - p->buf));
  658.             if (j > nbytes) j = (int)nbytes;
  659.             if (j > PIPESIZ - plen) j = PIPESIZ - plen;
  660.             nbytes -= j; plen += j;
  661.             bytes_written += j;
  662.             memcpy (pbuf, buf, j);
  663.             buf += j;
  664.             if (nbytes > 0 && plen < PIPESIZ)
  665.               {
  666.                 j = PIPESIZ - plen;
  667.                 if (j > nbytes) j = (int)nbytes;
  668.                 nbytes -= j; plen += j;
  669.                 bytes_written += j;
  670.                 memcpy (p->buf, buf, j);
  671.                 buf += j;
  672.               }
  673.             p->len = plen;
  674.         } else {        /* pipe full */
  675.             if (p->readers == 0 || p->readers == VIRGIN_PIPE) {
  676.             /* maybe some other signal is waiting for us? */
  677.                 check_sigs();
  678.                 DEBUG(("pipe_write: broken pipe"));
  679.                 raise(SIGPIPE);
  680.                 return EPIPE;
  681.             }
  682.             if (f->flags & O_NDELAY) {
  683.                 break;
  684.             }
  685.     /* is someone select()ing the other end of the pipe for reading? */
  686.             if (p->rsel) {
  687.                 wakeselect(p->rsel);
  688.             }
  689.             wake(IO_Q, (long)p);    /* readers may continue */
  690. DEBUG(("pipe_write: sleep on %lx", p));
  691.             sleep(IO_Q, (long)p);
  692.         }
  693.     }
  694.     this->time = timestamp;
  695.     this->date = datestamp;
  696.     if (bytes_written > 0) {
  697.         if (p->rsel) {
  698.             wakeselect(p->rsel);
  699.         }
  700.         wake(IO_Q, (long)p);    /* maybe someone wants this data */
  701.     }
  702.  
  703.     return bytes_written;
  704. }
  705.  
  706. static long ARGS_ON_STACK 
  707. pipe_read(f, buf, nbytes)
  708.     FILEPTR *f; char *buf; long nbytes;
  709. {
  710.     int plen, j;
  711.     struct fifo *this;
  712.     struct pipe *p;
  713.     long bytes_read = 0;
  714.     char *pbuf;
  715.  
  716.     this = (struct fifo *)f->fc.index;
  717.     p = (f->flags & O_HEAD) ? this->outp : this->inp;
  718.     if (!p) {
  719.         DEBUG(("pipe_read: read on the wrong end of a pipe"));
  720.         return EACCDN;
  721.     }
  722.  
  723.     while (nbytes > 0) {
  724.         plen = p->len;
  725.         if (plen > 0) {
  726.             pbuf = &p->buf[p->start];
  727.             /* j is the amount that can be read continuously */
  728.             j = PIPESIZ - p->start;
  729.             if (j > nbytes) j = (int)nbytes;
  730.             if (j > plen) j = plen;
  731.             nbytes -= j; plen -= j;
  732.             bytes_read += j;
  733.             p->start += j;
  734.             memcpy (buf, pbuf, j);
  735.             buf += j;
  736.             if (nbytes > 0 && plen > 0)
  737.               {
  738.                 j = plen;
  739.                 if (j > nbytes) j = (int)nbytes;
  740.                 nbytes -= j; plen -= j;
  741.                 bytes_read += j;
  742.                 p->start = j;
  743.                 memcpy (buf, p->buf, j);
  744.                 buf += j;
  745.               }
  746.             p->len = plen;
  747.             if (plen == 0 || p->start == PIPESIZ)
  748.               p->start = 0;
  749.         }
  750.         else if (p->writers <= 0 || p->writers == VIRGIN_PIPE) {
  751.             TRACE(("pipe_read: no more writers"));
  752.             break;
  753.         }
  754.         else if ((f->flags & O_NDELAY) ||
  755.                ((this->dosflags & FA_CHANGED) && bytes_read > 0) )
  756.         {
  757.             break;
  758.         }
  759.         else {
  760.     /* is someone select()ing the other end of the pipe for writing? */
  761.             if (p->wsel) {
  762.                 wakeselect(p->wsel);
  763.             }
  764.             wake(IO_Q, (long)p);    /* writers may continue */
  765.             sleep(IO_Q, (long)p);
  766.         }
  767.     }
  768.     if (bytes_read > 0) {
  769.         if (p->wsel) {
  770.             wakeselect(p->wsel);
  771.         }
  772.         wake(IO_Q, (long)p);    /* wake writers */
  773.     }
  774.     return bytes_read;
  775. }
  776.  
  777. static long ARGS_ON_STACK 
  778. pty_write(f, buf, nbytes)
  779.     FILEPTR *f; const char *buf; long nbytes;
  780. {
  781.     long bytes_written = 0;
  782.  
  783.     if (!nbytes)
  784.         return 0;
  785.     if (f->flags & O_HEAD)
  786.         return pipe_write(f, buf, nbytes);
  787.     if (nbytes != 4)
  788.         ALERT("pty_write: slave nbytes != 4");
  789.     bytes_written = pipe_write(f, buf+3, 1);
  790.     if (bytes_written == 1)
  791.         bytes_written = 4;
  792.     return bytes_written;
  793. }
  794.  
  795. static long ARGS_ON_STACK 
  796. pty_read(f, buf, nbytes)
  797.     FILEPTR *f; char *buf; long nbytes;
  798. {
  799.     long bytes_read = 0;
  800.  
  801.     if (!nbytes)
  802.         return 0;
  803.     if (!(f->flags & O_HEAD))
  804.         return pipe_read(f, buf, nbytes);
  805.     if (nbytes != 4)
  806.         ALERT("pty_read: master nbytes != 4");
  807.     bytes_read = pipe_read(f, buf+3, 1);
  808.     if (bytes_read == 1)
  809.         bytes_read = 4;
  810.     return bytes_read;
  811. }
  812.  
  813. static long ARGS_ON_STACK 
  814. pty_writeb(f, buf, nbytes)
  815.     FILEPTR *f; const char *buf; long nbytes;
  816. {
  817.     if (!nbytes)
  818.         return 0;
  819.     if (f->flags & O_HEAD)
  820.         return EUNDEV;
  821.     return pipe_write(f, buf, nbytes);
  822. }
  823.  
  824. static long ARGS_ON_STACK 
  825. pty_readb(f, buf, nbytes)
  826.     FILEPTR *f; char *buf; long nbytes;
  827. {
  828.     if (!nbytes)
  829.         return 0;
  830.     if (!(f->flags & O_HEAD))
  831.         return EUNDEV;
  832.     return pipe_read(f, buf, nbytes);
  833. }
  834.  
  835. static long ARGS_ON_STACK 
  836. pipe_ioctl(f, mode, buf)
  837.     FILEPTR *f; int mode; void *buf;
  838. {
  839.     struct pipe *p;
  840.     struct fifo *this;
  841.     struct flock *lck;
  842.  
  843.     long r;
  844.  
  845.     this = (struct fifo *)f->fc.index;
  846.  
  847.     switch(mode) {
  848.     case FIONREAD:
  849.         p = (f->flags & O_HEAD) ? this->outp : this->inp;
  850.         if (p == 0) return EINVFN;
  851.         r = p->len;
  852.         if (r == 0) {
  853.             if (p->writers <= 0 || p->writers == VIRGIN_PIPE) {
  854.                 DEBUG(("pipe FIONREAD: no writers"));
  855.     /* arguably, we should return 0 for EOF, but this would break MINIWIN and
  856.      * perhaps some other MultiTOS programs
  857.      */
  858.                 r = -1;
  859.             }
  860.         } else if (is_terminal(f)) {
  861.             if (!(f->flags & O_HEAD))
  862.                 r = r >> 2;        /* r /= 4 */
  863.             else if (this->tty->state & TS_HOLD)
  864.                 r = 0;
  865.         }
  866.         *((long *) buf) = r;
  867.         break;
  868.     case FIONWRITE:
  869.         p = (f->flags & O_HEAD) ? this->inp : this->outp;
  870.         if (p == 0) return EINVFN;
  871.         if (p->readers <= 0) {
  872.     /* see compatibility comment under FIONREAD */
  873.             r = -1;
  874.         } else {
  875.             r = PIPESIZ - p->len;
  876.             if (is_terminal(f)) {
  877.                 if (f->flags & O_HEAD)
  878.                     r = r >> 2;    /* r /= 4 */
  879.                 else if (this->tty->state & TS_HOLD)
  880.                     r = 0;
  881.             }
  882.         }
  883.         *((long *) buf) = r;
  884.         break;
  885.     case FIOEXCEPT:
  886.         *((long *) buf) = 0;
  887.         break;
  888.     case F_SETLK:
  889.     case F_SETLKW:
  890.         lck = (struct flock *)buf;
  891.         while (this->flags & O_LOCK) {
  892.             if (this->lockpid != curproc->pid) {
  893.                 DEBUG(("pipe_ioctl: pipe already locked"));
  894.                 if (mode == F_SETLKW && lck->l_type != F_UNLCK) {
  895.                     sleep(IO_Q, (long)this);        /* sleep a while */
  896.                 }
  897.                 else
  898.                     return ELOCKED;
  899.             } else
  900.                 break;
  901.         }
  902.         if (lck->l_type == F_UNLCK) {
  903.             if (!(f->flags & O_LOCK)) {
  904.                 DEBUG(("pipe_ioctl: wrong file descriptor for UNLCK"));
  905.                 return ENSLOCK;
  906.             }
  907.             this->flags &= ~O_LOCK;
  908.             this->lockpid = 0;
  909.             f->flags &= ~O_LOCK;
  910.             wake(IO_Q, (long)this);    /* wake up anyone waiting on the lock */
  911.         }
  912.         else {
  913.             this->flags |= O_LOCK;
  914.             this->lockpid = curproc->pid;
  915.             f->flags |= O_LOCK;
  916.         }
  917.         break;
  918.     case F_GETLK:
  919.         lck = (struct flock *)buf;
  920.         if (this->flags & O_LOCK) {
  921.             lck->l_type = F_WRLCK;
  922.             lck->l_start = lck->l_len = 0;
  923.             lck->l_pid = this->lockpid;
  924.         }
  925.         else
  926.             lck->l_type = F_UNLCK;
  927.         break;
  928.     case TIOCSTART:
  929.         if (is_terminal(f) && !(f->flags & O_HEAD) &&
  930. #if 0
  931.             NULL != (p = this->outp) && p->rsel && p->tail != p->head)
  932. #else
  933.             NULL != (p = this->outp) && p->rsel && p->len > 0)
  934. #endif
  935.             wakeselect (p->rsel);
  936.         break;
  937.     case TIOCFLUSH:
  938.         {
  939.         long flushtype;
  940.         long *which;
  941.  
  942.         which = (long *)buf;
  943.         if (!which || !(*which & 3))
  944.             flushtype = 3;
  945.         else
  946.             flushtype = *which;
  947.  
  948.         if ((flushtype & 1) && this->inp) {
  949.             this->inp->start = this->inp->len = 0;
  950.             wake(IO_Q, (long)this->inp);
  951.         }
  952.         if ((flushtype & 2) && this->outp) {
  953.             this->outp->start = this->outp->len = 0;
  954.             wake(IO_Q, (long)this->outp);
  955.         }
  956.         break;
  957.         }
  958.     case TIOCOUTQ:
  959.         p = (f->flags & O_HEAD) ? this->inp : this->outp;
  960.         assert(p != 0);
  961.         if (p->readers <= 0) {
  962.             r = -1;
  963.         } else {
  964.             r = p->len;
  965.             if (is_terminal(f) && (f->flags & O_HEAD))
  966.                 r = r >> 2;    /* r /= 4 */
  967.         }
  968.         *((long *) buf) = r;
  969.         break;
  970.     case TIOCIBAUD:
  971.     case TIOCOBAUD:
  972.         *(long *)buf = -1L;
  973.         break;
  974.     case TIOCGFLAGS:
  975.         *((unsigned short *)buf) = 0;
  976.         break;
  977.     case TCURSOFF:
  978.     case TCURSON:
  979.     case TCURSSRATE:
  980.     case TCURSBLINK:
  981.     case TCURSSTEADY:
  982.     case TCURSGRATE:
  983.     /* kludge: this assumes TOSWIN style escape sequences */
  984.         tty_putchar(f, (long)'\033', RAW);
  985.         switch (mode) {
  986.         case TCURSOFF:
  987.             tty_putchar(f, (long)'f', RAW);
  988.             break;
  989.         case TCURSON:
  990.             tty_putchar(f, (long)'e', RAW);
  991.             break;
  992.         case TCURSSRATE:
  993.             this->cursrate = *((int *)buf);
  994.             /* fall through */
  995.         case TCURSBLINK:
  996.             tty_putchar(f, (long)'t', RAW);
  997.             tty_putchar(f, (long)this->cursrate+32, RAW);
  998.             break;
  999.         case TCURSSTEADY:
  1000.             tty_putchar(f, (long)'t', RAW);
  1001.             tty_putchar(f, (long)32, RAW);
  1002.             break;
  1003.         case TCURSGRATE:
  1004.             return this->cursrate;
  1005.         }
  1006.         break;
  1007.     default:
  1008.     /* if the file is a terminal, Fcntl will automatically
  1009.      * call tty_ioctl for us to handle 'generic' terminal
  1010.      * functions
  1011.      */
  1012.         return EINVFN;
  1013.     }
  1014.  
  1015.     return 0;
  1016. }
  1017.  
  1018. static long ARGS_ON_STACK 
  1019. pipe_lseek(f, where, whence)
  1020.     FILEPTR *f; long where; int whence;
  1021. {
  1022.     UNUSED(f); UNUSED(where); UNUSED(whence);
  1023.     return EACCDN;
  1024. }
  1025.  
  1026. static long ARGS_ON_STACK 
  1027. pipe_datime(f, timeptr, rwflag)
  1028.     FILEPTR *f;
  1029.     short *timeptr;
  1030.     int rwflag;
  1031. {
  1032.     struct fifo *this;
  1033.  
  1034.     this = (struct fifo *)f->fc.index;
  1035.     if (rwflag) {
  1036.         this->time = timeptr[0];
  1037.         this->date = timeptr[1];
  1038.     }
  1039.     else {
  1040.         timeptr[0] = this->time;
  1041.         timeptr[1] = this->date;
  1042.     }
  1043.     return 0;
  1044. }
  1045.  
  1046. static long ARGS_ON_STACK 
  1047. pipe_close(f, pid)
  1048.     FILEPTR *f;
  1049.     int pid;
  1050. {
  1051.     struct fifo *this, *old;
  1052.     struct pipe *p;
  1053.     int rwmode;
  1054.     FILEPTR **old_x, *x;
  1055.  
  1056.     this = (struct fifo *)f->fc.index;
  1057.  
  1058.     if (f->links <= 0) {
  1059.  
  1060. /* wake any processes waiting on this pipe */
  1061.         wake(IO_Q, (long)this->inp);
  1062.         if (this->inp->rsel)
  1063.             wakeselect(this->inp->rsel);
  1064.         if (this->inp->wsel)
  1065.             wakeselect(this->inp->wsel);
  1066.  
  1067.         if (this->outp) {
  1068.             wake(IO_Q, (long)this->outp);
  1069.             if (this->outp->wsel)
  1070.                 wakeselect(this->outp->wsel);
  1071.             if (this->outp->rsel)
  1072.                 wakeselect(this->outp->rsel);
  1073.         }
  1074.  
  1075. /* remove the file pointer from the list of open file pointers 
  1076.  * of this pipe
  1077.  */
  1078.         old_x = &this->open;
  1079.         x = this->open;
  1080.         while (x && x != f) {
  1081.                 old_x = &x->next;
  1082.                 x = x->next;
  1083.         }
  1084.         assert(x);
  1085.         *old_x = f->next;
  1086.         /* f->next = 0; */
  1087.  
  1088.         rwmode = f->flags & O_RWMODE;
  1089.         if (rwmode == O_RDONLY || rwmode == O_RDWR) {
  1090.             p = (f->flags & O_HEAD) ? this->outp : this->inp;
  1091. /* note that this can never be a virgin pipe, since we had a handle
  1092.  * on it!
  1093.  */            if (p)
  1094.                 p->readers--;
  1095.         }
  1096.         if (rwmode == O_WRONLY || rwmode == O_RDWR) {
  1097.             p = (f->flags & O_HEAD) ? this->inp : this->outp;
  1098.             if (p) p->writers--;
  1099.         }
  1100.  
  1101. /* correct for the "selfread" flag (see pipe_creat) */
  1102.         if ((f->flags & O_HEAD) && !(this->dosflags & 0x02))
  1103.             this->inp->readers--;
  1104.  
  1105. /* check for locks */
  1106.         if ((f->flags & O_LOCK) && (this->lockpid == pid)) {
  1107.             this->flags &= ~O_LOCK;
  1108.             wake(IO_Q, (long)this);    /* wake up anyone waiting on the lock */
  1109.         }
  1110.     }
  1111.  
  1112. /* see if we're finished with the pipe */
  1113.     if (this->inp->readers == VIRGIN_PIPE)
  1114.         this->inp->readers = 0;
  1115.     if (this->inp->writers == VIRGIN_PIPE)
  1116.         this->inp->writers = 0;
  1117.  
  1118.     if (this->inp->readers <= 0 && this->inp->writers <= 0) {
  1119.         TRACE(("disposing of closed fifo"));
  1120. /* unlink from list of FIFOs */
  1121.         if (rootlist == this)
  1122.             rootlist = this->next;
  1123.         else {
  1124.             for (old = rootlist; old->next != this;
  1125.                     old = old->next) {
  1126.                 if (!old) {
  1127.                     ALERT("fifo not on list???");
  1128.                     return EINTRN;
  1129.                 }
  1130.             }
  1131.             old->next = this->next;
  1132.         }
  1133.         kfree(this->inp);
  1134.         if (this->outp) kfree(this->outp);
  1135.         if (this->tty) kfree(this->tty);
  1136.         kfree(this);
  1137.         pipetime = timestamp;
  1138.         pipedate = datestamp;
  1139.     }
  1140.  
  1141.     return 0;
  1142. }
  1143.  
  1144. static long ARGS_ON_STACK 
  1145. pipe_select(f, proc, mode)
  1146.     FILEPTR *f;
  1147.     long proc;
  1148.     int mode;
  1149. {
  1150.     struct fifo *this;
  1151.     struct pipe *p;
  1152.  
  1153.     this = (struct fifo *)f->fc.index;
  1154.  
  1155.     if (mode == O_RDONLY) {
  1156.         p = (f->flags & O_HEAD) ? this->outp : this->inp;
  1157.         if (!p) {
  1158.             DEBUG(("read select on wrong end of pipe"));
  1159.             return 0;
  1160.         }
  1161.  
  1162. /* NOTE: if p->writers <= 0 then reads won't block (they'll fail) */
  1163.         if ((p->len > 0 &&
  1164.             (!is_terminal(f) || !(f->flags & O_HEAD) ||
  1165.              !(this->tty->state & TS_HOLD))) ||
  1166.             p->writers <= 0) {
  1167.             return 1;
  1168.         }
  1169.  
  1170.         if (p->rsel)
  1171.             return 2;    /* collision */
  1172.         p->rsel = proc;
  1173.         if (is_terminal(f) && !(f->flags & O_HEAD))
  1174.             this->tty->rsel = proc;
  1175.         return 0;
  1176.     } else if (mode == O_WRONLY) {
  1177.         p = (f->flags & O_HEAD) ? this->inp : this->outp;
  1178.         if (!p) {
  1179.             DEBUG(("write select on wrong end of pipe"));
  1180.             return 0;
  1181.         }
  1182.         if ((p->len < PIPESIZ &&
  1183.             (!is_terminal(f) || (f->flags & O_HEAD) ||
  1184.              !(this->tty->state & TS_HOLD))) ||
  1185.             p->readers <= 0)
  1186.             return 1;    /* data may be written */
  1187.         if (p->wsel)
  1188.             return 2;    /* collision */
  1189.         p->wsel = proc;
  1190.         if (is_terminal(f) && !(f->flags & O_HEAD))
  1191.             this->tty->wsel = proc;
  1192.         return 0;
  1193.     }
  1194.     return 0;
  1195. }
  1196.  
  1197. static void ARGS_ON_STACK 
  1198. pipe_unselect(f, proc, mode)
  1199.     FILEPTR *f;
  1200.     long proc;
  1201.     int mode;
  1202. {
  1203.     struct fifo *this;
  1204.     struct pipe *p;
  1205.  
  1206.     this = (struct fifo *)f->fc.index;
  1207.  
  1208.     if (mode == O_RDONLY) {
  1209.         p = (f->flags & O_HEAD) ? this->outp : this->inp;
  1210.         if (!p) {
  1211.             return;
  1212.         }
  1213.         if (p->rsel == proc)
  1214.             p->rsel = 0;
  1215.     } else if (mode == O_WRONLY) {
  1216.         p = (f->flags & O_HEAD) ? this->inp : this->outp;
  1217.         if (!p) {
  1218.             return;
  1219.         }
  1220.         if (p->wsel == proc)
  1221.             p->wsel = 0;
  1222.     }
  1223. }
  1224.